package cn.doitedu.vueapi.dao.tg;


import cn.doitedu.vueapi.pojo.tg.AdFlowVo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;
import cn.doitedu.vueapi.pojo.tg.AdPvs;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public interface AdPvsMapper{

	@SelectProvider(type = SqlProvider.class, method = "selectByAdPvs")
	@ResultMap("BaseResultMap")
	List<AdPvs> selectByAdPvs(AdPvs adPvs);

	@SelectProvider(type = SqlProvider.class, method = "selectMyRpts")
	@Results(value={
			@Result(column = "dt",property = "dt"),
			@Result(column = "uid",property = "uid"),
			@Result(column = "adid",property = "adid"),
			@Result(column = "pvs",property = "pvs"),
			@Result(column = "uvs",property = "uvs")
	})
	List<AdFlowVo> selectMyRpts(AdFlowVo adFlowVo);

	@InsertProvider(type = SqlProvider.class,method = "insertAdPvs")
	int insertAdPvs(AdPvs adPvs);

	@UpdateProvider(type = SqlProvider.class,method = "updateAdPvs")
	int updateAdPvs(AdPvs adPvs);

	@DeleteProvider(type = SqlProvider.class,method = "deleteAdPvs")
	int deleteAdPvs(AdPvs adPvs);

	 class SqlProvider{
	
		public String selectByAdPvs(AdPvs adPvs){
			SQL sql= new SQL().SELECT("*").FROM("`ad_pvs`");
			if(adPvs.getId()!=null) sql.AND().WHERE("`id`="+adPvs.getId());
			if(adPvs.getAdid()!=null) sql.AND().WHERE("`adid`="+adPvs.getAdid());
			if(adPvs.getUid()!=null) sql.AND().WHERE("`uid`="+adPvs.getUid());
			if(adPvs.getIp()!=null) sql.AND().WHERE("`ip`='"+adPvs.getIp()+"'");
			if(adPvs.getDt()!=null) sql.AND().WHERE("`dt`='"+adPvs.getDt()+"'");

			return sql.toString();
		}

		public String selectMyRpts(AdFlowVo adFlowVo){
			StringBuilder sb = new StringBuilder();
			sb.append("select dt,uid,adid,count(1) as pvs,count(distinct ip) as uvs from ad_pvs where uid = ");
			sb.append(adFlowVo.getUid());
			sb.append(" group by dt,uid,adid");

			return sb.toString();
		}


		public String insertAdPvs(AdPvs adPvs){
			SQL sql= new SQL().INSERT_INTO("`ad_pvs`");
			if(adPvs.getId()!=null) sql.VALUES("`id`",String.valueOf(adPvs.getId()));
			if(adPvs.getAdid()!=null) sql.VALUES("`adid`",String.valueOf(adPvs.getAdid()));
			if(adPvs.getUid()!=null) sql.VALUES("`uid`",String.valueOf(adPvs.getUid()));
			if(adPvs.getIp()!=null) sql.VALUES("`ip`","'"+adPvs.getIp()+"'");
			if(adPvs.getDt()!=null) sql.VALUES("`dt`","'"+adPvs.getDt()+"'");
			return sql.toString();
		}
		public String updateAdPvs(AdPvs adPvs){
			SQL sql= new SQL().UPDATE("`ad_pvs`");
			if(adPvs.getId()!=null) sql.SET("`id`=" + adPvs.getId());
			if(adPvs.getAdid()!=null) sql.SET("`adid`=" + adPvs.getAdid());
			if(adPvs.getUid()!=null) sql.SET("`uid`=" + adPvs.getUid());
			if(adPvs.getIp()!=null) sql.SET("`ip`='" + adPvs.getIp()+"'");
			if(adPvs.getDt()!=null) sql.SET("`dt`='" + adPvs.getDt()+"'");
			sql.WHERE("`id`="+adPvs.getId());
			return sql.toString();	
		}
		public String deleteAdPvs(AdPvs adPvs){
			SQL sql= new SQL().DELETE_FROM("`ad_pvs`");
			if(adPvs.getId()!=null) sql.AND().WHERE("`id`=" + adPvs.getId());
			if(adPvs.getAdid()!=null) sql.AND().WHERE("`adid`=" + adPvs.getAdid());
			if(adPvs.getUid()!=null) sql.AND().WHERE("`uid`=" + adPvs.getUid());
			if(adPvs.getIp()!=null) sql.AND().WHERE("`ip`='" + adPvs.getIp()+"'");
			if(adPvs.getDt()!=null) sql.AND().WHERE("`dt`='" + adPvs.getDt()+"'");
			return sql.toString();
		}
	}
	
}